package com.lanqiao.attendance.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanqiao.attendance.pojo.Course;
import com.lanqiao.attendance.service.CourseService;
import com.lanqiao.attendance.vo.CourseByStuVo;
import com.lanqiao.attendance.vo.CourseVo;
import com.lanqiao.attendance.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("course")
@Api(tags = "排课信息接口")
@CrossOrigin
public class CourseContoller {
    @Autowired
    private CourseService courseService;

    @GetMapping
    @ApiOperation("根据课程id查看课程信息")
    public Result queryById(Long id) {
        return courseService.queryById(id);
    }

    @GetMapping("queryList")
    @ApiOperation("查询所有课程信息")
    public Result query() {
        return courseService.query();
    }

    @GetMapping("queryByNum")
    @ApiOperation("根据课程号查询课程信息")
    public Result queryByCourseNum(String num) {
        return courseService.queryByCourseNum(num);
    }

    @PostMapping("insertBySchedule")
    @ApiOperation("根据排课VO新增课程信息")
    public Result insertBySchedule(@RequestBody CourseVo courseVo) {
        System.out.println(courseVo.getTeaName());
        return courseService.insertBySchedule(courseVo);
    }
    @DeleteMapping("delete/{id}")
    @ApiOperation("根据课程id删除课程信息")
    public Result delete(@PathVariable("id") Long id) {
        return courseService.delete(id);
    }

    @PutMapping("update")
    @ApiOperation("更新课程信息")
    public Result update(@RequestBody Course course) {

        return courseService.update(course);
    }

    @GetMapping("schedule")
    @ApiOperation("查询排课信息列表")
    public Result querySchedule(Long id) {
        return courseService.querySchedule(id);
    }

    @GetMapping("CourseStu")
    @ApiOperation("学生查询上课信息列表")
    public Result queryCourseStu(@RequestParam("stuAccount") String stuAccount,
                                 @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                                 @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize) {

        Page page = PageHelper.startPage(pageNum,pageSize);
        List<CourseByStuVo> courses= courseService.queryCourseStu(stuAccount);
        PageInfo<CourseByStuVo> pageInfo = new PageInfo<>(page.getResult());
        if (courses==null){
            return Result.fail(400,"查询失败");
        }
        return Result.success("查询成功",pageInfo);
    }

    @GetMapping("CourseStuByTitle")
    @ApiOperation("学生根据课程名查询上课信息")
    public Result queryCourseStuByTitle(@RequestParam("stuAccount") String stuAccount, @RequestParam("couTitle") String couTitle,
                                        @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                                        @RequestParam(value = "pageSize",required = false,defaultValue = "2")Integer pageSize
    ) {


        List<CourseByStuVo> courseByStuVos= courseService.queryCourseStuByTitle(stuAccount, couTitle);
        int courseNum = courseByStuVos.size();
        Page page = PageHelper.startPage(pageNum,pageSize);
        courseByStuVos= courseService.queryCourseStuByTitle(stuAccount, couTitle);
        PageInfo<CourseByStuVo> pageInfo=new PageInfo<>(courseByStuVos);
        Object obj[] = new Object[]{courseByStuVos,courseNum};

        if (courseByStuVos==null){
            return Result.fail(400,"课程名无法查询到相关课程");
        }
        return Result.success("查询成功",obj);
    }

    @GetMapping("CourseDetail")
    @ApiOperation("学生查询上课详情信息")
    public Result queryCourseDetail(@RequestParam("couNumber") String couNumber) {
        return courseService.queryCourseDetail(couNumber);
    }
}
